package com.hs.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.server.domain.VehZnshzlzpxx;
import com.hs.server.mapper.VehZnshzlzpxxMapper;
import com.hs.server.service.VehZnshzlzpxxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author: chenjiarui
 * @Date: 2021-07-09 11:21
 */
@Service
@Slf4j
public class VehZnshzlzpxxServiceImpl extends ServiceImpl<VehZnshzlzpxxMapper, VehZnshzlzpxx> implements VehZnshzlzpxxService {
    @Autowired
    VehZnshzlzpxxMapper vehZnshzlzpxxMapper;

    @Override
    public synchronized void AddVehZnshzlzpxx(VehZnshzlzpxx vehZnshzlzpxx) {
        QueryWrapper<VehZnshzlzpxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lsh",vehZnshzlzpxx.getLsh());
        queryWrapper.eq("zpzl",vehZnshzlzpxx.getZpzl());
        if (vehZnshzlzpxxMapper.selectCount(queryWrapper)==0)
            vehZnshzlzpxxMapper.insert(vehZnshzlzpxx);
        else
            vehZnshzlzpxxMapper.update(vehZnshzlzpxx,queryWrapper);
    }

    @Override
    public VehZnshzlzpxx GetVehZnshzlzpxx(String lsh, String zpzl) {
        QueryWrapper<VehZnshzlzpxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lsh", lsh);
        queryWrapper.eq("zpzl", zpzl);
        //System.out.println(lsh+"------"+zpzl);
        return vehZnshzlzpxxMapper.selectOne(queryWrapper);
    }

    @Override
    public List<VehZnshzlzpxx> GetVehZnshzlzpxxListByLsh(String lsh) {
        QueryWrapper<VehZnshzlzpxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lsh", lsh);
        //queryWrapper.eq("shzt", "0");
        return vehZnshzlzpxxMapper.selectList(queryWrapper);
    }

    @Override
    public List<VehZnshzlzpxx> GetVehZnshzlzpxxListWithNotReviewed(String lsh) {
        QueryWrapper<VehZnshzlzpxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lsh", lsh);
        queryWrapper.eq("shzt", "0");
        return vehZnshzlzpxxMapper.selectList(queryWrapper);
    }

    @Override
    public synchronized int UpdateZnshzlzpxx(VehZnshzlzpxx vehZnshzlzpxx) {
        //synchronized 修饰方法时锁定的是调用该方法的对象。它并不能使调用该方法的多个对象在执行顺序上互斥。观察：注入进来的和SpringContextUtil.getBean的对象是否一致
        QueryWrapper<VehZnshzlzpxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lsh", vehZnshzlzpxx.getLsh());
        queryWrapper.eq("zpzl", vehZnshzlzpxx.getZpzl());
        //加判断是为了防止获取第二次照片的时候把第一次还在审核当中的shzt=0写进到第二次调用GetVehZnshzlzpxxListWithNotReviewed的list中，造成后面的shzt被改成0
//        if (!vehZnshzlzpxxMapper.selectOne(queryWrapper).getShzt().equals("0")){
//            log.info("UpdateZnshzlzpxx更新的照片shzt已不为0，已跳过！");
//            return 0;
//        }
        return vehZnshzlzpxxMapper.update(vehZnshzlzpxx,queryWrapper);
    }

    @Override
    public List<String> getShztsByLsh(String lsh) {
        return vehZnshzlzpxxMapper.getShztsByLsh(lsh);
    }

    @Override
    public List<String> getShztsByLshWithoutLinked(String lsh, String zpzls) {
        return vehZnshzlzpxxMapper.getShztsByLshWithoutLinked(lsh,zpzls);
    }

    @Override
    public int getNeedCount(String lsh) {
        return vehZnshzlzpxxMapper.getNeedCount(lsh);
    }

    @Override
    public int getAlreadyByLsh(String lsh) {
        return vehZnshzlzpxxMapper.getAlreadyByLsh(lsh);
    }

    @Override
    public List<String> getBtgZpzl(String lsh, String zpzls) {
        return vehZnshzlzpxxMapper.getBtgZpzl(lsh,zpzls);
    }

    @Override
    public List<String> getBtgZpzls(String lsh) {
        return vehZnshzlzpxxMapper.getBtgZpzls(lsh);
    }

    @Override
    public int updateLinkedShzt(String lsh, String shzt, String shbtgyy,String zpzl) {
        return vehZnshzlzpxxMapper.updateLinkedShzt(lsh,shzt,shbtgyy,zpzl);
    }
}
